package red.stu.mc.mod.codeblock.util;

public class CodeStringTool {
	
	// Default
	private static final int STATE_DEF = 0;
	// Single-Line Comments 
	private static final int STATE_S_NOTES_SINGLE = 1; // '//'
	// private static final int STATE_E_NOTES_SINGLE = -1; // '\n'
	// multiline comment
	private static final int STATE_S_NOTES_MULTILINE = 2; // '/*'
	// private static final int STATE_E_NOTES_MULTILINE = -2;// '*/'
	private static final int STATE_S_QUOTATION_SINGLE = 3; // '''
	private static final int STATE_S_QUOTATION_DOUBLE = 4; // '"'
	
	// import
	private static final int STATE_IMPORT = 5;
	
	public static String analysis(String code) {
		char[] codes = (" " + code + "\n").toCharArray();
		int state = STATE_DEF;
		int ps = 0;
		String word = "";
		String import_word = "";
		String import_other = "";
		for (int i = 0; i < codes.length; i++) {
			char n = codes[i];
			switch (n) {
			case '\'':
				if (codes[i - 1] != '\\') {
					if (state == STATE_DEF) {
						state = STATE_S_QUOTATION_SINGLE;
					} else if (state == STATE_S_QUOTATION_SINGLE) {
						// 单行注释结束
						state = STATE_DEF;
					}
				}
				break;
			case '\"':
				if (codes[i - 1] != '\\') {
					if (state == STATE_DEF) {
						state = STATE_S_QUOTATION_DOUBLE;
					} else if (state == STATE_S_QUOTATION_DOUBLE) {
						// 双引号结束
						state = STATE_DEF;
					}
				}
				break;
			case '/':
				if (state == STATE_DEF) {
					if (codes[i - 1] == '/') {
						state = STATE_S_NOTES_SINGLE;
						ps = i;
					}
				} else if (state == STATE_S_NOTES_MULTILINE) {
					if (codes[i - 1] == '*') {
						// 多行注释结束
						state = STATE_DEF;
						// ps;
					}
				}
				break;
			case '*':
				if (state == STATE_DEF) {
					if (codes[i - 1] == '/') {
						state = STATE_S_NOTES_MULTILINE;
						ps = i;
					}
				}
				break;
			case '\n':
			case ';':
				if (n == '\n') {
					if (state == STATE_S_NOTES_SINGLE) {
						// 单行注释结束
						// ps;
						state = STATE_DEF;
					}
				}
				if (state == STATE_IMPORT) {
					// import结束
					// import_other import_word
					if (import_other.length() > 6) { // 去除import关键词
						import_other = import_other.substring(0, import_other.length() - 6);
						String packName = import_word.trim();
						import_other += "var " + subENString(packName) 
							+ "=Java.type('" + packName + "')";
					}	
					import_word = "";
					state = STATE_DEF;
				}
				break;
			default:
				if (n >= '0' && n<= '9' // 0-9
						|| n >= 'a' && n <= 'z' // a-z
						|| n >= 'A' && n <= 'Z' // A-Z
						|| n == '_') { // _
					if (state == STATE_DEF) {
						word += n;
					}
				} else {
					word = "";
				}
				if (state == STATE_IMPORT) {
					import_word += n;
				}
				break;
			}
			if (state != STATE_IMPORT) {
				import_other += n;
			}
			if (word.equals("import")) {
				state = STATE_IMPORT;
			}
			
		}
		import_other = import_other.substring(1);
		if (import_other.charAt(import_other.length() - 1) == '\n')
			import_other = import_other.substring(0, import_other.length() - 1);
		return import_other;
	}
	
	private static String subENString(String str) {
		String[] packs = str.split("\\.");
		if (packs.length == 0)
			return str;
		else
			return packs[packs.length - 1];
	}
	
	public static void main(String[] args) {
		String code = CodeStringTool.analysis("function main() {\r\n" + 
				"	import java.lang.System;\r\n" + 
				"/*	// import net.minecraft.client.Minecraft;\r\n" + 
				"	\r\n" + 
				"	System.out.print(\"import java.lang.System;\");*/\r\n" + 
				"}");
		System.out.println("[" + code + "]");
		
	}
	
}
